package com.irdstudio.efp.console.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.irdstudio.efp.console.common.ConsoleConstant;
import com.irdstudio.efp.console.service.dao.PrdApplyRuleDao;
import com.irdstudio.efp.console.service.dao.PrdInfoApplyDao;
import com.irdstudio.efp.console.service.dao.PrdRuleRelDao;
import com.irdstudio.efp.console.service.dao.SDicDao;
import com.irdstudio.efp.console.service.domain.PrdApplyRule;
import com.irdstudio.efp.console.service.domain.PrdInfoApply;
import com.irdstudio.efp.console.service.domain.PrdRuleRel;
import com.irdstudio.efp.console.service.domain.SDic;
import com.irdstudio.efp.console.service.facade.PrdApplyRuleService;
import com.irdstudio.efp.console.service.facade.PrdCaseManageService;
import com.irdstudio.efp.console.service.facade.PrdChangeRecordService;
import com.irdstudio.efp.console.service.facade.PrdInfoApplyService;
import com.irdstudio.efp.console.service.facade.PrdInfoService;
import com.irdstudio.efp.console.service.facade.PrdRuleRelService;
import com.irdstudio.efp.console.service.facade.PubSysInfoService;
import com.irdstudio.efp.console.service.vo.PrdApplyRuleVO;
import com.irdstudio.efp.console.service.vo.PrdCaseManageVO;
import com.irdstudio.efp.console.service.vo.PrdChangeRecordVO;
import com.irdstudio.efp.console.service.vo.PrdInfoApplyVO;
import com.irdstudio.efp.console.service.vo.PrdInfoVO;
import com.irdstudio.efp.console.service.vo.PrdRuleRelVO;
import com.irdstudio.efp.flow.common.vo.PageApproveVO;
import com.irdstudio.basic.framework.core.base.AbstractFrameworkService;
import com.irdstudio.basic.framework.core.base.DataOptionalAuthorityDao;
import com.irdstudio.basic.framework.core.base.FrameworkService;
import com.irdstudio.basic.framework.core.constant.BaseConstant;
import com.irdstudio.basic.framework.core.constant.SequenceConstance;
import com.irdstudio.basic.framework.core.util.BeanCompareUtil;
import com.irdstudio.basic.framework.core.util.StringUtil;

/**
 * <p>
 * ServiceImpl: 产品审批信息表
 * <p>
 * 
 * @author fuzm
 * @date 2018-05-03
 */
@Service("prdInfoApplyService")
public class PrdInfoApplyServiceImpl extends AbstractFrameworkService implements PrdInfoApplyService, FrameworkService {

	private static Logger logger = LoggerFactory.getLogger(PrdInfoApplyServiceImpl.class);

	@Autowired
	private PrdInfoApplyDao prdInfoApplyDao;
	@Autowired
	private PrdInfoService prdInfoService;
	@Autowired
	private PubSysInfoService pubSysInfoService;

	@Autowired
	private PrdRuleRelDao prdRuleRelDao;

	@Autowired
	private PrdApplyRuleDao prdApplyRuleDao;
	@Autowired
	private SDicDao sDicDao;

	@Autowired
	@Qualifier("prdCaseManageService")
	private PrdCaseManageService prdCaseManageService;
	@Autowired
	@Qualifier("prdChangeRecordService")
	private PrdChangeRecordService prdChangeRecordService;
	@Autowired
	@Qualifier("prdApplyRuleService")
	private PrdApplyRuleService prdApplyRuleService;
	@Autowired
	@Qualifier("prdRuleRelService")
	private PrdRuleRelService prdRuleRelService;

	// @Autowired
	// @Qualifier("sDicService")
	// private SDicService sDicService;

	/**
	 * 新增操作
	 */
	@Override
	public int insertPrdInfoApply(PrdInfoApplyVO inPrdInfoApplyVo) {
		logger.debug("当前新增数据为:" + inPrdInfoApplyVo.toString());
		int num = 0;
		try {
			PrdInfoApply prdInfoApply = new PrdInfoApply();
			// 获取流水号
//			setInsertDefaultProperty(inPrdInfoApplyVo);
			beanCopy(inPrdInfoApplyVo, prdInfoApply);
			// 请求类型为产品上线时生成产品ID、产品编号
			String reqType = inPrdInfoApplyVo.getReqType();
			if (ConsoleConstant.REQ_TYPE_01.equals(reqType)) {
				// 获取产品主键
				String prdId = this.getSequence(SequenceConstance.PRD_INFO_SEQ_ID);
				// 获取产品编号
				String prdCode = this.getSequence(SequenceConstance.PRD_CODE_SEQ_ID);
				// 获取版本号
				// String prdVersion = this.getSequence(SequenceConstance.PRD_VERSION);
				prdInfoApply.setPrdId(prdId);
				prdInfoApply.setPrdCode(prdCode);
				// prdInfoApply.setPrdVersion(prdVersion);
			}
			num = prdInfoApplyDao.insertPrdInfoApply(prdInfoApply);
		} catch (Exception e) {
			logger.error("新增数据发生异常!", e);
			num = -1;
		}
		logger.debug("当前新增数据条数为:" + num);
		return num;
	}

	/**
	 * 删除操作
	 */
	@Override
	public int deleteByPk(PrdInfoApplyVO inPrdInfoApplyVo) {
		logger.debug("当前删除数据条件为:" + inPrdInfoApplyVo);
		int num = 0;
		try {
			PrdInfoApply prdInfoApply = new PrdInfoApply();
			beanCopy(inPrdInfoApplyVo, prdInfoApply);
			num = prdInfoApplyDao.deleteByPk(prdInfoApply);
		} catch (Exception e) {
			logger.error("删除数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:" + inPrdInfoApplyVo + "删除的数据条数为" + num);
		return num;
	}

	/**
	 * 更新操作
	 */
	@Override
	public int updateByPk(PrdInfoApplyVO inPrdInfoApplyVo) {
		logger.debug("当前修改数据为:" + inPrdInfoApplyVo.toString());
		int num = 0;
		try {
			PrdInfoApply prdInfoApply = new PrdInfoApply();
			setUpdateDefaultProperty(inPrdInfoApplyVo);
			beanCopy(inPrdInfoApplyVo, prdInfoApply);
			num = prdInfoApplyDao.updateByPk(prdInfoApply);
		} catch (Exception e) {
			logger.error("修改数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:" + inPrdInfoApplyVo + "修改的数据条数为" + num);
		return num;
	}

	/**
	 * 审批通过后处理
	 */
	@Override
	public void pass(String bizSerno, PageApproveVO pageApproveVo) {
		logger.info("产品审批通过流水号:" + bizSerno);
		int num = 0;
		//
		try {
			if (Objects.nonNull(bizSerno) && !"".equals(bizSerno)) {
				PrdInfoApply inPrdInfoApply = new PrdInfoApply();
				inPrdInfoApply.setBizSerno(bizSerno);
				inPrdInfoApply = prdInfoApplyDao.queryByPk(inPrdInfoApply);
				// beanCopy(inPrdInfoApply, inPrdInfoApplyVo);
				// inPrdInfoApplyVo.setOpenDay(pubSysInfoService.getOpenday(BaseConstant.SYS_ID_1001));//设置营业日期，用于设置生效日期
				inPrdInfoApply.setAprvUser(pageApproveVo.getAprvUserId());// 复核人
				inPrdInfoApply.setAprvDate(pubSysInfoService.getOpenday(BaseConstant.SYS_ID_1001));// 复核时间
				inPrdInfoApply.setAprvStatus(ConsoleConstant.APRV_STATUS_03);
				setUpdateDefaultProperty(inPrdInfoApply);
				// beanCopy(inPrdInfoApplyVo, prdInfoApply);
				// 修改申请为审批通过
				num = prdInfoApplyDao.updateByPk(inPrdInfoApply);
				logger.info("根据条件:" + bizSerno + "修改的数据条数为" + num);
				String oldPrdId = inPrdInfoApply.getPrdId();
				String reqType = inPrdInfoApply.getReqType();
				// 下线、变更时，更新原产品信息为失效
				if (ConsoleConstant.REQ_TYPE_02.equals(reqType) || ConsoleConstant.REQ_TYPE_03.equals(reqType)) {
					PrdInfoVO oldPrdVo = new PrdInfoVO();
					oldPrdVo.setPrdId(oldPrdId);
					oldPrdVo.setPrdStatus(ConsoleConstant.PRD_STATUS_04);
					oldPrdVo.setExpiryDate(pubSysInfoService.getOpenday(BaseConstant.SYS_ID_1001));// 失效日期
					num = prdInfoService.updateByPk(oldPrdVo);
					logger.info("根据条件:" + oldPrdVo + "修改的数据条数为" + num);
				}
				// 停用时，更新原产品信息为停用
				if (ConsoleConstant.REQ_TYPE_04.equals(reqType)) {
					// 第一步，更新关联生效专案为停用
					PrdCaseManageVO prdCaseManageVo = new PrdCaseManageVO();
					prdCaseManageVo.setPrdId(oldPrdId);
					prdCaseManageVo.setCaseStatus(ConsoleConstant.CASE_STATUS_03);
					prdCaseManageService.updateCaseStatusByPrdId(prdCaseManageVo);
					logger.info("根据条件:" + oldPrdId + "修改的专案数据条数为" + num);
					// 第二步，更新产品状态为已停用
					PrdInfoVO oldPrdVo = new PrdInfoVO();
					oldPrdVo.setPrdId(oldPrdId);
					oldPrdVo.setPrdStatus(ConsoleConstant.PRD_STATUS_03);
					num = prdInfoService.updateByPk(oldPrdVo);
					logger.info("根据条件:" + oldPrdVo + "修改的数据条数为" + num);
				}
				// 插入新的产品信息
				if (ConsoleConstant.REQ_TYPE_01.equals(reqType) || ConsoleConstant.REQ_TYPE_02.equals(reqType)) {
					// PrdInfoApply queryPrdInfoApply = new PrdInfoApply();
					// beanCopy(inPrdInfoApplyVo, queryPrdInfoApply);
					// prdInfoApply = prdInfoApplyDao.queryByPk(queryPrdInfoApply);
					// beanCopy(prdInfoApply, inPrdInfoApplyVo);
					// PrdInfoVO prdInfoVO = prdInfoService.buildPrdInfoVO(inPrdInfoApplyVo);
					PrdInfoVO prdInfoVO = new PrdInfoVO();
					beanCopy(inPrdInfoApply, prdInfoVO);
					prdInfoVO.setPrdStatus(ConsoleConstant.PRD_STATUS_02);// 已生效
					prdInfoVO.setEffictiveDate(pubSysInfoService.getOpenday(BaseConstant.SYS_ID_1001));// 生效日期=营业日期
					// 产品变更,生成新的产品ID
					if (ConsoleConstant.REQ_TYPE_02.equals(reqType)) {
						try {
							// 获取产品主键
							String prdId = this.getSequence(SequenceConstance.PRD_INFO_SEQ_ID);
							prdInfoVO.setPrdId(prdId);
							String prdVersion = inPrdInfoApply.getPrdVersion();// 版本号
							int v = Integer.parseInt(prdVersion);
							v++;
							prdVersion = Integer.toString(v);
							prdInfoVO.setPrdVersion(prdVersion);// 版本号
						} catch (Exception e) {
							logger.info("获取流水号失败!" + e.toString());
						}
					}
					// setUserInfoToVO(prdInfoVO);
					num = prdInfoService.insertPrdInfo(prdInfoVO);
					logger.info("插入的数据条数为" + num);

					// 插入产品规则关联信息
					PrdApplyRuleVO paramRuleVO = new PrdApplyRuleVO();
					paramRuleVO.setBizSerno(bizSerno);
					List<PrdApplyRule> ruleList = prdApplyRuleDao.queryAllByBizSerno(paramRuleVO);
					if (ruleList != null) {
						for (int i = 0; i < ruleList.size(); i++) {
							PrdApplyRule rule = ruleList.get(i);
							PrdRuleRel ruleRel = new PrdRuleRel();
							beanCopy(rule, ruleRel);

							// 插入产品规则关联信息表
							ruleRel.setPrdId(prdInfoVO.getPrdId());
							prdRuleRelDao.insertPrdRuleRel(ruleRel);
						}
					}
				}
			} else {
				throw new Exception("流水号不能为空");
			}
		} catch (Exception e) {
			logger.error("修改数据发生异常!", e);
			num = -1;
		}
	}

	/**
	 * 审批不通过后处理
	 */
	@Override
	public void refuse(String bizSerno, PageApproveVO pageApproveVo) {
		logger.info("产品审批不通过流水号:" + bizSerno);
		int num = 0;
		try {
			if (Objects.nonNull(bizSerno) && !"".equals(bizSerno)) {
				PrdInfoApply inPrdInfoApply = new PrdInfoApply();
				inPrdInfoApply.setBizSerno(bizSerno);
				inPrdInfoApply = prdInfoApplyDao.queryByPk(inPrdInfoApply);
				inPrdInfoApply.setAprvUser(pageApproveVo.getAprvUserId());// 复核人
				inPrdInfoApply.setAprvDate(pubSysInfoService.getOpenday(BaseConstant.SYS_ID_1001));// 复核时间
				inPrdInfoApply.setAprvStatus(ConsoleConstant.APRV_STATUS_04);
				setUpdateDefaultProperty(inPrdInfoApply);
				// 修改申请为审批通过
				num = prdInfoApplyDao.updateByPk(inPrdInfoApply);
				logger.info("根据条件:" + bizSerno + "修改的数据条数为" + num);
			} else {
				throw new Exception("流水号不能为空");
			}
		} catch (Exception e) {
			logger.error("修改数据发生异常!", e);
			num = -1;
		}
	}

	@Override
	public void repulse(String bizSerno, PageApproveVO pageApproveVo) {

	}

	/**
	 * 查询操作
	 */
	@Override
	public PrdInfoApplyVO queryByPk(PrdInfoApplyVO inPrdInfoApplyVo) {

		logger.debug("当前查询参数信息为:" + inPrdInfoApplyVo);
		try {
			PrdInfoApply queryPrdInfoApply = new PrdInfoApply();
			beanCopy(inPrdInfoApplyVo, queryPrdInfoApply);
			PrdInfoApply queryRslPrdInfoApply = prdInfoApplyDao.queryByPk(queryPrdInfoApply);
			if (Objects.nonNull(queryRslPrdInfoApply)) {
				PrdInfoApplyVO outPrdInfoApplyVo = beanCopy(queryRslPrdInfoApply, new PrdInfoApplyVO());
				logger.debug("当前查询结果为:" + outPrdInfoApplyVo.toString());
				return outPrdInfoApplyVo;
			} else {
				logger.debug("当前查询结果为空!");
			}
		} catch (Exception e) {
			logger.error("查询数据发生异常!", e);
		}
		return null;
	}

	/**
	 * 查询用户权限数据
	 */
	@Override
	public List<PrdInfoApplyVO> queryAllOwner(PrdInfoApplyVO prdInfoApplyVo) {

		logger.debug("当前查询本人所属数据信息的参数信息为:" + prdInfoApplyVo);
		PrdInfoApply prdInfoApply = beanCopy(prdInfoApplyVo, new PrdInfoApply());
		List<PrdInfoApplyVO> list = null;
		try {
			List<PrdInfoApply> prdInfoApplys = prdInfoApplyDao.queryAllOwnerByPage(prdInfoApply);
			logger.debug("当前查询本人所属数据信息的结果集数量为:" + prdInfoApplys.size());
			pageSet(prdInfoApplys, prdInfoApply);
			list = (List<PrdInfoApplyVO>) beansCopy(prdInfoApplys, PrdInfoApplyVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}

		return list;

	}

	/**
	 * 查询当前机构权限数据
	 */
	@Override
	public List<PrdInfoApplyVO> queryAllCurrOrg(PrdInfoApplyVO prdInfoApplyVo) {

		logger.debug("当前查询本人所属机构数据信息的参数信息为:" + prdInfoApplyVo);
		PrdInfoApply prdInfoApply = beanCopy(prdInfoApplyVo, new PrdInfoApply());
		List<PrdInfoApply> prdInfoApplys = prdInfoApplyDao.queryAllCurrOrgByPage(prdInfoApply);
		logger.debug("当前查询本人所属机构数据信息的结果集数量为:" + prdInfoApplys.size());
		List<PrdInfoApplyVO> list = null;
		try {
			pageSet(prdInfoApplys, prdInfoApply);
			list = (List<PrdInfoApplyVO>) beansCopy(prdInfoApplys, PrdInfoApplyVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}

		return list;

	}

	/**
	 * 查询当前机构及下属机构权限数据
	 */
	@Override
	public List<PrdInfoApplyVO> queryAllCurrDownOrg(PrdInfoApplyVO prdInfoApplyVo) {

		logger.debug("当前查询本人所属机构及以下数据信息的参数信息为:" + prdInfoApplyVo);
		PrdInfoApply prdInfoApply = beanCopy(prdInfoApplyVo, new PrdInfoApply());
		List<PrdInfoApply> prdInfoApplys = prdInfoApplyDao.queryAllCurrDownOrgByPage(prdInfoApply);
		logger.debug("当前查询本人所属机构及以下数据信息的结果集数量为:" + prdInfoApplys.size());
		List<PrdInfoApplyVO> list = null;
		try {
			pageSet(prdInfoApplys, prdInfoApply);
			list = (List<PrdInfoApplyVO>) beansCopy(prdInfoApplys, PrdInfoApplyVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}

		return list;

	}

	/**
	 * 根据产品ID查询待发起、审批中的数据
	 */
	@Override
	public List<PrdInfoApplyVO> queryChangingPrdByPrdId(String prdId) {
		logger.debug("参数信息为:" + prdId);
		List<PrdInfoApply> prdInfoApplys = prdInfoApplyDao.queryChangingPrdByPrdId(prdId);
		logger.debug("当前查询本人所属机构及以下数据信息的结果集数量为:" + prdInfoApplys.size());
		List<PrdInfoApplyVO> list = null;
		try {
			list = (List<PrdInfoApplyVO>) beansCopy(prdInfoApplys, PrdInfoApplyVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}

		return list;
	}

	/**
	 * 根据产品编号查询产品变动历史记录
	 */
	@Override
	public List<PrdInfoApplyVO> queryPrdInfoApplyDetails(PrdInfoApplyVO prdInfoApplyVo) {
		logger.debug("参数信息为:" + prdInfoApplyVo);
		PrdInfoApply prdInfoApply = beanCopy(prdInfoApplyVo, new PrdInfoApply());
		List<PrdInfoApply> prdInfoApplys = prdInfoApplyDao.queryPrdInfoApplyDetailsByPage(prdInfoApply);
		logger.debug("结果集数量为:" + prdInfoApplys.size());
		List<PrdInfoApplyVO> list = null;
		try {
			pageSet(prdInfoApplys, prdInfoApply);
			list = (List<PrdInfoApplyVO>) beansCopy(prdInfoApplys, PrdInfoApplyVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}

		return list;
	}

	@Override
	public int insertPrdChangeRecords(PrdInfoApplyVO prdInfoApplyVo) {
		int num = 0;
		try {
			// 获取创建人和创建时间
			setInsertDefaultProperty(prdInfoApplyVo);
			String prdId = prdInfoApplyVo.getPrdId();
			// 根据产品编号获取变更前产品信息
			PrdInfoVO prdInfoVo = new PrdInfoVO();
			prdInfoVo.setPrdId(prdId);
			prdInfoVo = prdInfoService.queryByPk(prdInfoVo);
			PrdChangeRecordVO prdChangeRecordVo = new PrdChangeRecordVO();
			// 不需要比较的字段
			String[] nonCompare = { "loginUserOrgLocation", "loginUserOrgCode", "loginUserId", "loginUserLeageOrgCode", "legalOrgCode",
					"legalOrgCodeName", "reqType", "reqDesc", "aprvStatus", "prdId", "prdCode", "prdVersion", "currentAprvUser", "aprvUser",
					"aprvDate", "createTime", "createUser", "lastUpdateUser", "lastUpdateTime", "prdFilDir", "prdSelDir", "prdThlDri",
					"orgCode", "contTemplateName", "eleNucFormName", "rateSchemeName", "crdApprFlowName", "crdMeaMedolName",
					"rateAdjustType" };
			// 字段ID转换字段描述
			Map<String, String> descMap = new HashMap<String, String>();
			descMap.put("prdDesc", "产品描述");
			descMap.put("crdAprvMinAmt", "单笔最小金额(含)(元)");
			descMap.put("crdAprvMaxAmt", "单笔最高金额(含)(元)");
			descMap.put("loanAppMinAmt", "最小支用金额(含)(元)");
			descMap.put("loanAppMaxAmt", "最高支用金额(含)(元)");
			descMap.put("cyclicWay", "额度是否循环");
			descMap.put("creditTerm", "授信期限值");
			descMap.put("creditTermUnit", "授信期限单位");
			descMap.put("repayWay", "还款方式");
			descMap.put("inteDay", "结息日");
			descMap.put("repayDayType", "还款日类型");
			descMap.put("repayDay", "还款日");
			descMap.put("guarWay", "担保方式");
			descMap.put("loanUseType", "贷款用途种类");
			descMap.put("allowAmount", "是否允许提额");
			descMap.put("contTemplateId", "合同模板");
			descMap.put("contTemplateName", "合同模板");
			descMap.put("modContFormId", "提额合同模板");
			descMap.put("creditFormId", "授信表单");
			descMap.put("amtApprFlowId", "提额流程");
			descMap.put("amtMeaMedolId", "提额测算模型");
			// descMap.put("rateAdjustType", "利率调整方式");
			descMap.put("rateSchemeId", "利率方案");
			descMap.put("rateSchemeName", "利率方案 ");
			descMap.put("eleNucFormId", "电核表单");
			descMap.put("eleNucFormName", "电核表单");
			descMap.put("crdApprFlowId", "业务流程");
			descMap.put("crdApprFlowName", "业务流程");
			descMap.put("crdMeaMedolId", "额度测算模型");
			descMap.put("crdMeaMedolName", "额度测算模型");
			descMap.put("repayCycleType", "还款周期");
			descMap.put("isAlongLoan", "是否随借随还");
			descMap.put("isUpload", "是否上传凭证");
			descMap.put("limitAmt", "产品限额(万元)");
			// 需要转换成字典名称的字段
			Map<String, String> sdicMap = new HashMap<String, String>();
			sdicMap.put("cyclicWay", "STD_ZX_YES_NO");
			sdicMap.put("creditTermUnit", "STD_ZB_TERM_TYPE");
			sdicMap.put("repayWay", "STD_PRD_REPAY_MODE");
			sdicMap.put("repayDayType", "REPAY_DAY_TYPE");
			sdicMap.put("guarWay", "STD_ZB_ASSURE_MEANS");
			sdicMap.put("loanUseType", "LOAN_USE_TYPE");
			sdicMap.put("allowAmount", "STD_ZX_YES_NO");
			// sdicMap.put("rateAdjustType", "STD_ZB_IREXE_TYPE");
			sdicMap.put("repayCycleType", "STD_ZB_REPAY_FREQ");
			sdicMap.put("isAlongLoan", "STD_ZX_YES_NO");
			sdicMap.put("isUpload", "STD_ZX_YES_NO");
			// 需要拼编号+名称的字段
			Map<String, String> popMap = new HashMap<String, String>();
			popMap.put("contTemplateId", "contTemplateName");
			popMap.put("eleNucFormId", "eleNucFormName");
			popMap.put("rateSchemeId", "rateSchemeName");
			popMap.put("crdApprFlowId", "crdApprFlowName");
			popMap.put("crdMeaMedolId", "crdMeaMedolName");
			// popMap.put("creditFormId", "creditFormName");
			// popMap.put("grantNo", "grantName");

			Map<String, Map<String, Object>> changesMap = BeanCompareUtil.beanDiff(prdInfoApplyVo, prdInfoVo, nonCompare, popMap);
			// 主表的字段变更，循环插入变更记录表
			for (String key : changesMap.keySet()) {
				Map<String, Object> change = changesMap.get(key);
				String oldValue = change.get(BeanCompareUtil.KEY_OLD_VALUE) != null ? change.get(BeanCompareUtil.KEY_OLD_VALUE).toString()
						: "";
				String newValue = change.get(BeanCompareUtil.KEY_NEW_VALUE) != null ? change.get(BeanCompareUtil.KEY_NEW_VALUE).toString()
						: "";
				// 判断是否需要字典转换
				if (sdicMap.containsKey(key)) {
					String[] t1 = oldValue.split(",");
					SDic sDic = new SDic();
					String oldValueTemp = "";
					if (t1.length > 0) {
						for (String s : t1) {
							sDic.setOpttype(sdicMap.get(key));
							sDic.setEnname(s);
							sDic = sDicDao.queryOneByennameOpttype(sDic);
							if (sDic != null) {
								oldValueTemp = oldValueTemp + sDic.getCnname() + ",";
							}
						}
					}
					oldValue = oldValueTemp.substring(0, oldValueTemp.length() - 1);
					String[] t2 = newValue.split(",");
					String newValueTemp = "";
					if (t2.length > 0) {
						for (String s : t2) {
							sDic.setOpttype(sdicMap.get(key));
							sDic.setEnname(s);
							sDic = sDicDao.queryOneByennameOpttype(sDic);
							if (sDic != null) {
								newValueTemp = newValueTemp + sDic.getCnname() + ",";
							}
						}
					}
					newValue = newValueTemp.substring(0, newValueTemp.length() - 1);
				}
				prdChangeRecordVo.setBizSerno(prdInfoApplyVo.getBizSerno());
				prdChangeRecordVo.setPrdId(prdInfoApplyVo.getPrdId());
				prdChangeRecordVo.setColumnId(key);
				prdChangeRecordVo.setColumnName(descMap.get(key));
				prdChangeRecordVo.setOldValue(oldValue);
				prdChangeRecordVo.setNewValue(newValue);
				prdChangeRecordVo.setCreateTime(prdInfoApplyVo.getCreateTime());
				prdChangeRecordVo.setCreateUser(prdInfoApplyVo.getCreateUser());
				int c = prdChangeRecordService.insertPrdChangeRecord(prdChangeRecordVo);
				num += c;
				logger.info("当前新增数据条数为:" + c + "," + prdChangeRecordVo);
			}
			logger.info("主表总新增数据条数为:" + num);
			// 判断子表变更：规则
			PrdApplyRuleVO prdApplyRuleVo = new PrdApplyRuleVO();
			prdApplyRuleVo.setBizSerno(prdInfoApplyVo.getBizSerno());
			// 查询关联新规则
			List<PrdApplyRuleVO> prdApplyRules = prdApplyRuleService.queryAllByBizSerno(prdApplyRuleVo);
			List<PrdApplyRuleVO> prdApplyRules_t = new ArrayList<PrdApplyRuleVO>();
			PrdRuleRelVO prdRuleRelVo = new PrdRuleRelVO();
			prdRuleRelVo.setPrdId(prdId);
			// 查询原关联规则
			List<PrdRuleRelVO> prdRules = prdRuleRelService.queryAllByPrdId(prdRuleRelVo);
			List<PrdRuleRelVO> prdRules_t = new ArrayList<PrdRuleRelVO>();
			Map<String, Object> m_o = new HashMap<String, Object>();
			Map<String, Object> m_n = new HashMap<String, Object>();
			// 比较新旧规则的变动
			if (prdApplyRules != null && prdApplyRules.size() > 0) {
				for (int i = 0; i < prdApplyRules.size(); i++) {
					PrdApplyRuleVO vo = prdApplyRules.get(i);
					String ruleCollId = vo.getRuleCollId();
					m_n.put(ruleCollId, vo);
				}
				if (prdRules != null && prdRules.size() > 0) {
					for (int j = 0; j < prdRules.size(); j++) {
						PrdRuleRelVO rule = prdRules.get(j);
						String ruleCollId_old = rule.getRuleCollId();
						m_o.put(ruleCollId_old, rule);
					}
				}
				for (String k : m_n.keySet()) {
					if (!m_o.containsKey(k)) {
						prdApplyRules_t.add((PrdApplyRuleVO) m_n.get(k));
					}
				}
				for (String k : m_o.keySet()) {
					if (!m_n.containsKey(k)) {
						prdRules_t.add((PrdRuleRelVO) m_o.get(k));
					}
				}
				// 将发生变化的规则列表插入变更表
				Map<String, String> sameRuleCollType = new HashMap<>();
				if (prdApplyRules_t != null && prdApplyRules_t.size() > 0) {
					for (int i = 0; i < prdApplyRules_t.size(); i++) {
						String oldValue = "";
						PrdApplyRuleVO vo = prdApplyRules_t.get(i);
						String ruleCollType = vo.getRuleCollType();// 规则类型
						if (prdRules_t != null && prdRules_t.size() > 0) {
							for (int j = 0; j < prdRules_t.size(); j++) {
								PrdRuleRelVO vo1 = prdRules_t.get(j);
								String ruleCollType1 = vo1.getRuleCollType();// 规则类型
								// 同一种规则类型
								if (ruleCollType.equals(ruleCollType1)) {
									oldValue = vo1.getRuleCollId();
									if (StringUtil.isNotEmpty(vo1.getRuleCollDesc())) {
										oldValue = vo1.getRuleCollId() + "," + vo1.getRuleCollDesc();
									}
									sameRuleCollType.put(ruleCollType, ruleCollType);
								}
							}
						}
						String newValue = vo.getRuleCollId();
						if (StringUtil.isNotEmpty(vo.getRuleCollDesc())) {
							newValue = vo.getRuleCollId() + "," + vo.getRuleCollDesc();
						}
						prdChangeRecordVo.setBizSerno(prdInfoApplyVo.getBizSerno());
						prdChangeRecordVo.setPrdId(prdInfoApplyVo.getPrdId());
						SDic sDic = new SDic();
						sDic.setOpttype("RULE_TYPE");
						sDic.setEnname(ruleCollType);
						sDic = sDicDao.queryOneByennameOpttype(sDic);
						prdChangeRecordVo.setColumnId(ruleCollType);
						prdChangeRecordVo.setColumnName(sDic != null ? sDic.getCnname() : "");
						prdChangeRecordVo.setOldValue(oldValue);
						prdChangeRecordVo.setNewValue(newValue);
						prdChangeRecordVo.setCreateTime(prdInfoApplyVo.getCreateTime());
						prdChangeRecordVo.setCreateUser(prdInfoApplyVo.getCreateUser());
						int c = prdChangeRecordService.insertPrdChangeRecord(prdChangeRecordVo);
						logger.info("当前新增数据条数为:" + c + "," + prdChangeRecordVo);
					}
				}
				if (prdRules_t != null && prdRules_t.size() > 0) {
					for (int i = 0; i < prdRules_t.size(); i++) {
						PrdRuleRelVO vo = prdRules_t.get(i);
						String ruleCollType = vo.getRuleCollType();// 规则类型
						// 存在该规则类型则不再插入
						if (!sameRuleCollType.containsValue(ruleCollType)) {
							prdChangeRecordVo.setBizSerno(prdInfoApplyVo.getBizSerno());
							prdChangeRecordVo.setPrdId(prdInfoApplyVo.getPrdId());
							SDic sDic = new SDic();
							sDic.setOpttype("RULE_TYPE");
							sDic.setEnname(ruleCollType);
							sDic = sDicDao.queryOneByennameOpttype(sDic);
							prdChangeRecordVo.setColumnId(ruleCollType);
							prdChangeRecordVo.setColumnName(sDic != null ? sDic.getCnname() : "");
							String oldValue = vo.getRuleCollId();
							if (StringUtil.isNotEmpty(vo.getRuleCollDesc())) {
								oldValue = vo.getRuleCollId() + "," + vo.getRuleCollDesc();
							}
							prdChangeRecordVo.setOldValue(oldValue);
							prdChangeRecordVo.setNewValue("");
							prdChangeRecordVo.setCreateTime(prdInfoApplyVo.getCreateTime());
							prdChangeRecordVo.setCreateUser(prdInfoApplyVo.getCreateUser());
							int c = prdChangeRecordService.insertPrdChangeRecord(prdChangeRecordVo);
							logger.info("当前新增数据条数为:" + c + "," + prdChangeRecordVo);
						}

					}
				}
			} else {
				if (prdRules != null && prdRules.size() > 0) {
					for (int i = 0; i < prdRules.size(); i++) {
						PrdRuleRelVO vo = prdRules.get(i);
						String ruleCollType = vo.getRuleCollType();// 规则类型
						prdChangeRecordVo.setBizSerno(prdInfoApplyVo.getBizSerno());
						prdChangeRecordVo.setPrdId(prdInfoApplyVo.getPrdId());
						SDic sDic = new SDic();
						sDic.setOpttype("RULE_TYPE");
						sDic.setEnname(ruleCollType);
						sDic = sDicDao.queryOneByennameOpttype(sDic);
						prdChangeRecordVo.setColumnId(ruleCollType);
						prdChangeRecordVo.setColumnName(sDic != null ? sDic.getCnname() : "");
						String oldValue = vo.getRuleCollId();
						if (StringUtil.isNotEmpty(vo.getRuleCollDesc())) {
							oldValue = vo.getRuleCollId() + "," + vo.getRuleCollDesc();
						}
						prdChangeRecordVo.setOldValue(oldValue);
						prdChangeRecordVo.setNewValue("");
						prdChangeRecordVo.setCreateTime(prdInfoApplyVo.getCreateTime());
						prdChangeRecordVo.setCreateUser(prdInfoApplyVo.getCreateUser());
						int c = prdChangeRecordService.insertPrdChangeRecord(prdChangeRecordVo);
						logger.info("当前新增数据条数为:" + c + "," + prdChangeRecordVo);
					}
				}
			}
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		return num;
	}

	@Override
	public DataOptionalAuthorityDao getDataAuthorityDao() {
		return this.prdInfoApplyDao;
	}
}
